export default {
	// 提取对象数组中的对象id
	arr2Id: function(arr) {
		return arr && arr.map(item => item.id ? item.id : item);
	},
	// 通过id数组来还原对象数组
	id2Arr: function(idArr, arr) {
		return idArr && idArr.map(id => {
			let curr = id;
			arr && arr.forEach(item => item.id === id && (curr = item));
			return curr;
		})
	},
	// 交集
	intersection: function(arr1, arr2) {
		let mergeArr = arr1 && arr2 && arr1.concat(arr2);
		arr1 = this.arr2Id(arr1);
		arr2 = this.arr2Id(arr2);
		let idArr = arr1 && arr1.filter(item => arr2.includes(item));
		return this.id2Arr(idArr, mergeArr);
	},
	// 并集
	union: function(arr1, arr2) {
		let mergeArr = arr1 && arr2 && arr1.concat(arr2);
		let _arr1 = this.arr2Id(arr1);
		let _arr2 = this.arr2Id(arr2);
		let idArr = [...new Set(_arr1.concat(_arr2))];
		return this.id2Arr(idArr, mergeArr);
	},
	// 补集
	complement: function(arr1, arr2) {
		let mergeArr = arr1 && arr2 && arr1.concat(arr2);
		arr1 = this.arr2Id(arr1);
		arr2 = this.arr2Id(arr2);
		let idArr = arr1.filter(item => !arr2.includes(item));
		return this.id2Arr(idArr, mergeArr);
	},
	// 从数组中，删除value
	removeValue: function(arr, value) {
		for (let index in arr) {
			let item = arr[index];
			if (item == value) {
				arr.splice(index, 1);
			}
		}
	},
	// 查找数据中value的位置，如果没有找到返回-1
	indexOf: function(arr, value) {
		for (let index in arr) {
			let item = arr[index];
			if (item == value) {
				return index;
			}
		}
		return -1;
	},
	// 从数组中，删除values
	removeValues: function(arr, values) {
		for (let index in values) {
			let item = values[index];
			this.removeValue(arr, item);
		}
	},
	addValues: function(arr, values) {
		console.log('add=========1==', arr, values);
		let that = this;
		for (let index in values) {
			let value = values[index];
			if (that.indexOf(arr, value) <= -1) {
				arr.push(value);
			}
		}
		console.log('add=========2==', arr, values);
	},
	sort: function(arr, mode /*desc,asc*/ ) {
		let sr = arr.sort(function(a, b) {
			switch (mode) {
				case "desc":
					return b - a;
				case "asc":
					return a - b;
			}
			return 0
		});
		return sr;
	}
}
